﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class CharImage : UserControl
    {
        public static readonly DependencyProperty GraphicUriProperty = DependencyProperty.Register(
            "GraphicUri", typeof(Uri), typeof(CharImage), new PropertyMetadata(null, new PropertyChangedCallback(GraphicUriChanged)));

        public Uri GraphicUri
        {
            get { return (Uri)GetValue(GraphicUriProperty); }
            set { SetValue(GraphicUriProperty, value); }
        }
        
        private static void GraphicUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CharImage t = (CharImage)d;
            t.GraphicUri = e.NewValue as Uri;
            t.Dispatcher.BeginInvoke(() => t.UpdateUri());
        }

        public void UpdateUri()
        {
            Uri uri = GraphicUri;
            
            if (uri == null)
                return;

            img.Source = new BitmapImage(uri);
        }
        
        public CharImage()
        {            
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            GraphicUri = new Uri("/UserChat.png", UriKind.Relative);
        }
    }
}